define(function(require, exports, module) {
	'use strict';
    //require('./notiPanelCtrl');
    //var rk = require('rk');
    //
    //var getTimeToString = require('core/services/noticeService');
    
    var rk = require('rk');
    
    
    var uService = require('core/services/biz/noticeService');
    var appsTypeConstants = uService.appsTypeConstants;
    var getTimeToString = uService.getTimeToString;

    $.widget('rk.notiCurtainCtrl', $.rk.oaCurtainCtrl, {
        _language: "zh",
        _create: function () {
            var me = this;
            var elem = me.element;
            if (navigator.userLanguage) {
                me._language = navigator.userLanguage.substring(0, 2).toLowerCase();
            } else {
                me._language = navigator.language.substring(0, 2).toLowerCase();
            }
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var type = opt.type;
            me.loadData(type);
            me._closeListener();
        },
        _closeListener: function () {
            var me = this;
            var elem = me.element;
            elem.on('click', ".oa-curtain-close", function () {
                me.closeMe();
            });
        },
        loadData: function (type, param) {
            var me = this;
            var elem = me.element;
            //加载提醒
            if (type === "remind") {
                if (!param) {
                    param = {pageNo: 1, pageSize: 20};
                }
                elem.beLoading();
                rk.post("/json/oa_notice/remind-data.action", param, function (json) {
                    if (json.status === 0) {
                        var dataList = json.remindList;
                        var listHtml = "";
                        if (dataList.length > 0) {
                            var tempArray = [];
                            $.each(dataList,function(i,dataJson){
                                if(me.appsTypeConstants[dataJson.notice.appType]) {
                                    var tempName = me.appsTypeConstants[dataJson.notice.appType].tpl;
                                    tempArray[i] = "oa/tmpl/notice/template/tpl_" + tempName + ".tpl";
                                }
                            });
                            var showLists = function(type,listHtml,elem) {
                                var temp = require('oa/tmpl/notice/template/tpl_AllRemind_table.tpl');
                                var html = rk.templateText(temp,{rows: listHtml});
                                elem.html(html);
                                elem.find('[act="notice_page"]').rkPaging({
                                    pagenum: json.pageNo,  //（可选）当前页数，默认1
                                    pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                    rowscount: json.dataCount, //所有数据数
                                    onChange: function (pagenum) {
                                        var params = param;
                                        params.pageNo = pagenum;
                                        params.pageSize = json.pageSize;
                                        me.loadData(type, params)
                                    }
                                });
                                elem.find('[act="beScroll"]').beScroll();
                            }
                            require.async(tempArray, function() {
                                var textArray = $.makeArray(arguments);//根据传的模版名，获取到返回的模版内容
                                $.each(textArray,function(i,text){
                                    var tempHtml = rk.templateText(text,dataList[i]);
                                    listHtml += me.getNoticeRowHtml(json.nowTime, dataList[i], tempHtml, dataList[i].user);
                                });
                                showLists(type,listHtml,elem);
                            });
                        } else {
                            //var html = rk.template("tpl_notice_none_curtain", {text: rk.i18n('NOTICE_NO_REMIND')});.
                            var temp = require('oa/tmpl/notice/tpl_notice_none_curtain.tpl');
                            var html = rk.templateText(temp, {text: rk.i18n('NOTICE_NO_REMIND')});
                            elem.html(html);
                        }
                    } else {
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                });
            }
            if (type === "notice") {
                if (!param) {
                    param = {pageNo: 1, pageSize: 20};
                }
                elem.beLoading();
                //加载系统通知
                rk.post("/json/oa_notice/notice-data.action", param, function (json) {
                    if (json.status === 0) {
                        var dataList = json.noticeList;
                        var listHtml = "";
                        if (dataList.length > 0) {
                            /*$.each(dataList, function (i, dataJson) {
                                var tempName = me.appsTypeConstants[dataJson.notice.appType].tpl;
                                //var tempHtml = rk.template("tpl_" + tempName, dataJson);
                                var tempHtml = "";
                                listHtml += me.getNoticeRowHtml(json.nowTime, dataJson, tempHtml, dataJson.user);
                            });
                            //var html = rk.template("tpl_AllNotice_table", {rows: listHtml});
                            var html = "";
                            elem.html(html);
                            elem.find('[act="notice_page"]').rkPaging({
                                pagenum: json.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.pageSize;
                                    me.loadData(type, params)
                                }
                            });*/
                            var tempArray = [];
                            $.each(dataList,function(i,dataJson){
                                if(me.appsTypeConstants[dataJson.notice.appType]) {
                                    var tempName = me.appsTypeConstants[dataJson.notice.appType].tpl;
                                    tempArray[i] = "oa/tmpl/notice/template/tpl_" + tempName + ".tpl";
                                }
                            });
                            var showLists = function(type,listHtml,elem) {
                                var temp = require('oa/tmpl/notice/template/tpl_AllNotice_table.tpl');
                                var html = rk.templateText(temp,{rows: listHtml});
                                elem.html(html);
                                elem.find('[act="notice_page"]').rkPaging({
                                    pagenum: json.pageNo,  //（可选）当前页数，默认1
                                    pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                    rowscount: json.dataCount, //所有数据数
                                    onChange: function (pagenum) {
                                        var params = param;
                                        params.pageNo = pagenum;
                                        params.pageSize = json.pageSize;
                                        me.loadData(type, params)
                                    }
                                });
                                elem.find('[act="beScroll"]').beScroll();
                                var notice_panel_rows = elem.find("[act='notice_panel_row']");
                                for(var i = 0 ; i<notice_panel_rows.length ; i++){
                                    var row = $(notice_panel_rows[i]);
                                    var hidden = row.find("[hrefType='no_click']");
                                    var hiddenDel = row.find("[hrefType='deleteHref']");
                                    if(hidden && hidden.length >0){
                                        row.css("cursor","default");
                                    }
                                    if(hiddenDel && hiddenDel.length >0){
                                        row.css("cursor","default");
                                    }
                                }
                            };
                            require.async(tempArray, function() {
                                var textArray = $.makeArray(arguments);//根据传的模版名，获取到返回的模版内容
                                $.each(textArray,function(i,text){
                                    var tempHtml = rk.templateText(text,dataList[i]);
                                    listHtml += me.getNoticeRowHtml(json.nowTime, dataList[i], tempHtml, dataList[i].user);
                                });
                                showLists(type,listHtml,elem);
                            });
                        } else {
                            //var html = rk.template("tpl_notice_none_curtain", {text: rk.i18n('NOTICE_NO_NOTICE')});
                            var temp = require('oa/tmpl/notice/tpl_notice_none_curtain.tpl');
                            var html = rk.templateText(temp, {text: rk.i18n('NOTICE_NO_NOTICE')});
                            elem.html(html);
                        }
                    } else {
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                });
            }
            if (type === "atme") {
                if (!param) {
                    param = {pageNo: 1, pageSize: 20};
                }
                elem.beLoading();
                //加载@我的
                rk.post("/json/oa_at/ats.action", param, function (json) {
                    if (json.status === 0) {
                        var dataList = json.body.atList;
                        var listHtml = "";
                        if (dataList.length > 0) {
                            $.each(dataList, function (i, dataJson) {
                                var tempHtml = "";
                                var user;
                                if (dataJson.type == 1 && dataJson.comment) {
                                    dataJson.comment.content = me._contentToUserAt(dataJson.comment.content, dataJson.comment.ats);
                                    //tempHtml = rk.template("tpl_atme_comment_table_row", dataJson.comment);
                                    var temp = require('oa/tmpl/notice/template/tpl_atme_comment_table_row.tpl');
                                    tempHtml = rk.templateText(temp, dataJson.comment);
                                    user = dataJson.comment.user;
                                } else if (dataJson.type == 0 && dataJson.feed) {
                                    dataJson.feed.content = me._contentToUserAt(dataJson.feed.content, dataJson.feed.ats);
                                    //tempHtml = rk.template("tpl_atme_feed_table_row", dataJson.feed);
                                    var temp = require('oa/tmpl/notice/template/tpl_atme_feed_table_row.tpl');
                                    tempHtml = rk.templateText(temp, dataJson.feed);
                                    user = dataJson.feed.user;
                                }
                                listHtml += me.getAtMeRowHtml(json.nowTime, dataJson, tempHtml, user);
                            });
                            //var html = rk.template("tpl_Atme_table", {rows: listHtml});
                            var temp = require('oa/tmpl/notice/template/tpl_Atme_table.tpl');
                            var html = rk.templateText(temp,{rows: listHtml});
                            elem.html(html);
                            elem.find('[act="announcement_page"]').rkPaging({
                                pagenum: json.body.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.body.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.body.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.body.pageSize;
                                    me.loadData(type, params)
                                }
                            });
                        } else {
                            //var html = rk.template("tpl_notice_none_curtain", {text: rk.i18n('NOTICE_NO_ATME')});
                            var temp = require('oa/tmpl/notice/tpl_notice_none_curtain.tpl');
                            var html = rk.templateText(temp, {text: rk.i18n('NOTICE_NO_ATME')});
                            elem.html(html);
                        }
                    } else {
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                    elem.find('[act="beScroll"]').beScroll();
                });
            }
        },
        getNoticeRowHtml: function (nowTime, dataJson, tempHtml, user) {
            var me = this;
            if (user) {
                if (!user.smallIcon) {
                    user.smallIcon = rk.DEFAULT_USER_ICON;
                }
            }
            //var html = rk.template("tpl_NoticeRow_table", {user:user,time:me.getTimeToString(dataJson.notice.createdAt,nowTime,me),tempHtml:tempHtml});
            //var html = rk.template("tpl_NoticeRow_table", {user:user,time:me.getTimeToString(dataJson.notice.createdAt,nowTime,me),tempHtml:tempHtml});
            var temp = require('oa/tmpl/notice/template/tpl_NoticeRow_table.tpl');
            var html = rk.templateText(temp,{user:user,time:me.getTimeToString(dataJson.notice.createdAt,nowTime,me),tempHtml:tempHtml});
            return html;
        },
        getAtMeRowHtml: function (nowTime, dataJson, tempHtml, user) {
            var me = this;
            var elem = me.element;
            var time;
            if (dataJson.feed) {
                time = dataJson.feed.date;
            } else if (dataJson.comment) {
                time = dataJson.comment.date;
            }
            if (user) {
                if (!user.icon) {
                    user.icon = rk.DEFAULT_USER_ICON;
                }
            }
            //var html = rk.template("tpl_AtMeRow_table", {user:user,time:me.getTimeToString(time,nowTime,me),tempHtml:tempHtml});
            var temp = require('oa/tmpl/notice/template/tpl_AtMeRow_table.tpl');
            var html = rk.templateText(temp,{user:user,time:me.getTimeToString(time,nowTime,me),tempHtml:tempHtml});
            return html;
        },
        getTimeToString: getTimeToString,
        _contentToUserAt: function (content, ats) {
            if (content && content.length > 100) {
                content = content.substr(0, 100);
                content += "...";
            }
            if (ats) {
                content += " ";
                $.each(ats, function (i, item) {
                    content = content.replace("@"+item.name+" ","<a href='/final/user.action?uid="+item.uid+"' hrefType='user' ucard='uid="+item.uid+"' target='_blank' uid='"+item.uid+"'>@"+item.name+"</a> ");
                });
                content = content.substr(0, content.length - 1);
                return content;
            } else {
                return content;
            }
        },
        appsTypeConstants: appsTypeConstants
    });
});